package com.ruoyi.manage.service.impl;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.constant.ManageRedisConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.sign.Md5Utils;
import com.ruoyi.manage.domain.User;
import com.ruoyi.manage.domain.request.LoginRequest;
import com.ruoyi.manage.mapper.UserMapper;
import com.ruoyi.manage.service.IAppUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

/**
 * @Author: wwy
 * @Date: 2021/1/5 13:48
 */
@Service
public class AppUserServiceImpl implements IAppUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 登录接口
     * @param loginRequest
     */
    @Override
    public boolean login(LoginRequest loginRequest, SysUser sysUser) {
        if(loginRequest == null || loginRequest.getType() == null){
            throw new CustomException("参数错误",HttpStatus.BAD_REQUEST);
        }
        if(loginRequest.getType() == 0){
            //账号密码登录
            if(StringUtils.isEmpty(loginRequest.getUsername()) || StringUtils.isEmpty(loginRequest.getPassword())){
                throw new CustomException("参数错误",HttpStatus.BAD_REQUEST);
            }
            //账号验证
            User user = userMapper.findByUsername(loginRequest.getUsername());
            if(user == null){
                //无此账号记录
                return false;
            }
            //进行密码验证
            try {
                sysUser.setUserId(user.getId());
                return Md5Utils.checkpassword(loginRequest.getPassword(), user.getPassword());
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        if(loginRequest.getType() == 1){
            //账号验证
            User user = userMapper.findByUsername(loginRequest.getUsername());
            if(user == null){
                //无此账号记录
                return false;
            }
            //短信登录
            Object verificationCodeCache = redisTemplate.opsForValue().get(ManageRedisConstants.LOGIN_CODE + loginRequest.getUsername());
            if(verificationCodeCache != null){
                String verificationCodeCacheStr = String.valueOf(verificationCodeCache);
                if(verificationCodeCacheStr.equals(loginRequest.getVerificationCode())){
                    sysUser.setUserId(user.getId());
                    //验证码正确
                    return true;
                }else{
                    throw new CustomException("验证码错误",HttpStatus.ERROR);
                }
            }
        }
        return false;
    }
}
